package com.huifaapp.mall.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.huifaapp.mall.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;

/**
 * @author : gongdaocai
 * @date : 16/8/30
 * FileName:
 * @description:
 */


public class ShareDialog extends Dialog implements View.OnClickListener {


    private RelativeLayout rl_share_wxhy, rl_share_pyq, rl_share_xlwb, rl_share_qqhy;
    private Button btn_share_cancel;
    private Context context;
    private String title;

    private String content;

    private String imageUrl;

    private String url;

    private Bitmap imageBitmap;
    private UMWeb web;
    private onShaerListener shaerListener;

    public ShareDialog(@NonNull Context context) {

        super(context, R.style.centerDialog_style);
        this.context=context;
        initView();

    }

    public void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_share, null);
        setContentView(view);
        rl_share_wxhy = (RelativeLayout) view.findViewById(R.id.rl_share_wxhy);
        rl_share_pyq = (RelativeLayout) view.findViewById(R.id.rl_share_pyq);
        rl_share_xlwb = (RelativeLayout) view.findViewById(R.id.rl_share_xlwb);
        rl_share_qqhy = (RelativeLayout) view.findViewById(R.id.rl_share_qqhy);
        btn_share_cancel = (Button) view.findViewById(R.id.btn_share_cancel);

        rl_share_wxhy.setOnClickListener(this);
        rl_share_pyq.setOnClickListener(this);
        rl_share_xlwb.setOnClickListener(this);
        rl_share_qqhy.setOnClickListener(this);
        btn_share_cancel.setOnClickListener(this);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//		lp.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        dialogWindow.setWindowAnimations(R.style.promptdialog_anim); //设置窗口弹出动画
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
//        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }






    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.rl_share_wxhy:
                ShaerWeixinPengYou();
                break;
            case R.id.rl_share_pyq:
                ShaerWeixinPengYouQuan();
                break;
            case R.id.rl_share_xlwb:
                ShaerWeiBo();
                break;
            case R.id.rl_share_qqhy:
                ShaerQQ();
                break;
            case R.id.btn_share_cancel:
                dismiss();
                break;
        }
        this.dismiss();
    }










    /**
     * 分享微信朋友圈
     */
    private void ShaerWeixinPengYouQuan() {
        new ShareAction((Activity) context).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).
                setCallback(new UMShareCallback("微信朋友圈"))
                 .withMedia(web)
                .share();
    }

    ;

    /**
     * 微信好友
     */

    private void ShaerWeixinPengYou() {

        new ShareAction((Activity) context)
                .setPlatform(SHARE_MEDIA.WEIXIN).
                setCallback(new UMShareCallback("微信"))
                .withMedia(web)
                .share();

    }

    /**
     * ShaerQQ
     *
     * @param
     * @return
     */
    private void ShaerQQ() {

        new ShareAction((Activity) context)
                .setPlatform(SHARE_MEDIA.QQ).
                setCallback(new UMShareCallback("QQ"))
                .withMedia(web)
                .share();

    }

    /**
     * 分享 QQ空间
     */
    private void ShaerQQKongJian() {


        new ShareAction((Activity) context)
                .setPlatform(SHARE_MEDIA.QZONE).
                setCallback(new UMShareCallback("QQ空间"))
                .withMedia(web)
                .share();
    }

    private void ShaerWeiBo() {


        new ShareAction((Activity) context)
                .setPlatform(SHARE_MEDIA.SINA).
                setCallback(new UMShareCallback("新浪微博"))
                .withMedia(web)
                .share();
    }



    public interface onShaerListener {
        public void onCancelu(String name);

        public void onErro(String name);

        public void onComplete(String name);

    }






    public  ShareDialog  setWeb(UMWeb web){
       this.web = web;
        return this;

    }




    public onShaerListener getShaerListener() {
        return shaerListener;
    }

    public ShareDialog setShaerListener(onShaerListener shaerListener) {
        this.shaerListener = shaerListener;
        return this;
    }




    class  UMShareCallback implements UMShareListener {
            private   String typeName;

        private  LoadingDialog loadingDialog;

        public UMShareCallback(String typeName) {
            this.typeName = typeName;
            loadingDialog = new LoadingDialog(context);
        }

        public void onStart(SHARE_MEDIA share_media) {
            if (loadingDialog != null&&!loadingDialog.isShowing()) {
                loadingDialog.show();
            }
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (loadingDialog != null&&loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
            shaerListener.onComplete(typeName);
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (loadingDialog != null&&loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
            shaerListener.onErro(typeName);
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            if (loadingDialog != null&&loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
            shaerListener.onCancelu(typeName);
        }
    }


}
